#include <windows.h>
#include "rsrc.inc"

extern int index;
extern bool stopped;
extern bool paused;
extern bool playing;
extern file* currentPlay;
extern TCHAR totalTime[];
extern TCHAR currentTime[];
extern int index;
#define TIMELONG 10
#define EXTLENGTH 5 //The length of file extension


void play(file *file){
	
TCHAR cmd[MAX_PATH+10];
TCHAR shortName[MAX_PATH];
TCHAR errBuff[MAX_PATH];
int mci=0;

GetShortPathName(file->path,shortName,MAX_PATH);
wsprintf(cmd,"play %s",shortName);
mci=mciSendString(cmd,"",NULL,NULL);
paused=false;
stopped=false;
playing=true;
currentPlay=file;
SetDlgItemText(NULL,IDC_EDTNAME,file->name);
}

void stop(file *file){
TCHAR cmd[MAX_PATH+10];
TCHAR shortName[MAX_PATH];
GetShortPathName(file->path,shortName,MAX_PATH);
wsprintf(cmd,"stop %s",shortName);
mciSendString(cmd,"",NULL,NULL);	
stopped=true;
paused=false;
playing=false;
currentPlay=file;
}

void pause(file *file){
TCHAR cmd[MAX_PATH+10];
TCHAR shortName[MAX_PATH];
GetShortPathName(file->path,shortName,MAX_PATH);
wsprintf(cmd,"pause %s",shortName);
mciSendString(cmd,"",NULL,NULL);
stopped=false;
paused=true;
playing=false;
currentPlay=file;
}

void setVolumn(){

}

void getTotal(HWND hwnd){
	TCHAR cmd[MAX_PATH+10];
	TCHAR shortName[MAX_PATH];
	TCHAR buff[MAX_PATH];
	GetShortPathName(currentPlay->path,shortName,MAX_PATH);
	wsprintf(cmd,TEXT("status %s length"),shortName);
	
	mciSendString(cmd,buff,MAX_PATH,NULL);
	
	long t=atol(buff);
	int min=t/(1000*60);
	int sec=t%(1000*60)/1000;
		wsprintf(totalTime,TEXT("%02d:%02d"),min,sec);
}

void getCurrent(HWND hwnd){
		TCHAR cmd[MAX_PATH+10];
	TCHAR shortName[MAX_PATH];
	TCHAR buff[MAX_PATH];
	GetShortPathName(currentPlay->path,shortName,MAX_PATH);
	wsprintf(cmd,TEXT("status %s position"),shortName);
	
	mciSendString(cmd,buff,MAX_PATH,NULL);
	
	long t=atol(buff);
	int min=t/(1000*60);
	int sec=t%(1000*60)/1000;
	TCHAR timeChar[TIMELONG];
	wsprintf(currentTime,TEXT("%02d:%02d"),min,sec);
//	MessageBox(hwnd,currentTime,TEXT(""),MB_OK);

}
void setTimeText(HWND hwnd){
	TCHAR t[2*TIMELONG];
	lstrcpy(t,currentTime);
	lstrcat(t,"\/");
	lstrcat(t,totalTime);
 SetDlgItemText(hwnd,IDC_EDTTIME,t);
}

bool filter(file *file){
TCHAR ext[EXTLENGTH];
int i=lstrlen(file->name)-1;
for(;i>=0;i--){
if(file->name[i]=='.')break;
}
LPTCH p=file->name;
p+=i;
lstrcpy(ext,p);
if(lstrcmp(ext,"mp3")!=0 && lstrcmp(ext,"wmv")!=0 && lstrcmp(ext,"wmv")!=0)
return false;
else return true;



}
